package cn.fl.ass.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.fl.ass.service.IAssChnBankAccService;
import cn.fl.ass.transform.AssChnBankAccVOTransform;
import cn.fl.ass.vo.AssChnBankAccVO;
import cn.fl.framework.constant.DictionaryConstant;
import fl.ass.facade.api.IAssChnBankAccApi;
import fl.ass.facade.dto.AssChnBankAccDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description: 拖车渠道银行账户信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssChnBankAccServiceImpl implements IAssChnBankAccService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAssChnBankAccService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

    @Autowired
    private IAssChnBankAccApi assChnBankAccApi;

    /**
     * 本地数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public AssChnBankAccVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
        return AssChnBankAccVOTransform.toVO(this.assChnBankAccApi.selectById(id));
	}

	@Override
	public Long add(AssChnBankAccVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		return this.assChnBankAccApi.add(AssChnBankAccVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.assChnBankAccApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AssChnBankAccVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		return this.assChnBankAccApi.modify(AssChnBankAccVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
        Pager pager = this.assChnBankAccApi.searchListPage(page);
        List<AssChnBankAccDTO> resultList = (List<AssChnBankAccDTO>)pager.getResultList();
        if (CollectionUtils.isNotEmpty(resultList)) {
            //账户类型
            Map<String, String> accTypCdMap = sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_CST_NP_ACC_TYP_CD);
            //银行名称
            Map<String, String> bankTypCdMap = sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_CST_NP_BK_CD);
            for (AssChnBankAccDTO assChnBankAccDTO : resultList) {
                assChnBankAccDTO.setAccTypCdNm(accTypCdMap.get(assChnBankAccDTO.getAccTypCd()));
                assChnBankAccDTO.setBkNm(bankTypCdMap.get(assChnBankAccDTO.getBkCd()));
            }
        }
        pager.setResultList(resultList);
        return pager;
    }

    @Override
    public boolean enable(Long id, Integer isValidInt) throws BaseException {
        if(null == id){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        };
        return this.assChnBankAccApi.enable(id, isValidInt);
    }

    @Override
    public List<KeyValueVO> searchCdList(String grpCd) throws BaseException {
        return this.getKvList(grpCd);
    }

    @Override
    public AssChnBankAccVO getByChannelId(Long id) throws BaseException {
        if(StringUtils.isEmpty(id)){
            return null;
        }
        AssChnBankAccVO assChnBankAccVO = AssChnBankAccVOTransform.toVO(this.assChnBankAccApi.getByChannelId(id));
        if (null != assChnBankAccVO) {
            //银行名称
            Map<String, String> bankTypCdMap = sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_CST_NP_BK_CD);
            assChnBankAccVO.setBkNm(bankTypCdMap.get(assChnBankAccVO.getBkCd()));
        }
        return assChnBankAccVO;
    }

    /**
     * 根据字典中代码组值获取组员集合
     * @param grpCd 代码组值
     * @return 集合
     */
    private List<KeyValueVO> getKvList(String grpCd){
        List<KeyValueVO> kvList = new ArrayList<>();
        try {
            List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
            if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
                for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
                    KeyValueVO keyValueVO = new KeyValueVO();
                    keyValueVO.setKey(sysDictionaryDTO.getCode());
                    keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
                    kvList.add(keyValueVO);
                }
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("获取字典项组失败 BaseException").setThrowable(e).setKeys("grpCd:" + grpCd).info();
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取字典项组失败 Exception").setThrowable(e).setKeys("grpCd:" + grpCd).info();
        }
        return kvList;
    }

}
